<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/3/8
 * Time: 14:41
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Bowithdraw extends Base
{
    /**
     * 脖脸皮提现列表
     */
    public function index(){
        if(request()->isAjax ()){
            $bw_number = trim(input('bw_number'), ' '); // 提现单号
            $user_username = trim(input('user_username')); // 用户名称
            $user_phone = trim(input('user_phone')); // 手机号
            $start_time = input('start_time');
            $end_time = input('end_time');
            $bw_status = input('bw_status');
            
            $where = [];
            if(isset($bw_number) && $bw_number != ''){
                $where['bw_number'] = ['like', '%' . $bw_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_real_name'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bw_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bw_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bw_time'] = ['<=', $end_time];
            }
            if(isset($bw_status) && $bw_status != ''){
                $where['bw_status'] = $bw_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_withdraw')
                ->join('up_u_user', 'bw_user_id = user_id', 'LEFT')
                ->where($where)
                ->count('bw_id');
            $lists = Db::table('up_bo_withdraw')
                ->join('up_u_user', 'bw_user_id = user_id', 'LEFT')
                ->field('bw_id, bw_status, bw_type, bw_number, bw_price, bw_refuse_reason, bw_remit, 
                bw_time, user_phone, user_bank_username, user_bank_name, user_bankcard_code, 
                bw_binte_id, user_real_name, bw_admin_id')
                ->where($where)
                ->order('bw_time desc')
                ->page($Nowpage,$limits)
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    if($v['bw_type'] == 1){
                        $zhuan_number = Db::table('up_bo_withdraw_to_integral')
                            ->where('bwi_bw_id', $v['bw_id'])
                            ->sum('bwi_price');
                        $lists[$k]['real_price'] = $v['bw_price'] - $zhuan_number;
                    }else{
                        $lists[$k]['real_price'] = 0;
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 提现详情
     * @return mixed
     */
    public function bwDetail(){
        $bw_id = input('bw_id');
        $detail = Db::table('up_bo_withdraw')
            ->join('up_u_user', 'bw_user_id = user_id', 'LEFT')
            ->field('user_real_name, user_phone, bw_status, bw_refuse_reason, bw_type, bw_number, 
            bw_price, bw_time, bw_check_time, user_bank_name, user_bank_username, user_bankcard_code')
            ->where('bw_id', $bw_id)
            ->find();
        if($detail['bw_type'] == 1){
            $zhuan_number = Db::table('up_bo_withdraw_to_integral')->where('bwi_bw_id', $bw_id)->sum('bwi_price');
            $detail['real_price'] = $detail['bw_price'] - $zhuan_number;
        }else{
            $detail['real_price'] = 0;
        }
        return $this->fetch('', ['detail' => $detail]);
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function bwAllow(){
        $bw_id = input('id');
        /**
         * 修改申请状态
         * 提现到银行卡：无其他操作
         * 提现到积分：更新用户积分、入积分明细
         */
        $withdraw = Db::table('up_bo_withdraw')
            ->where('bw_id', $bw_id)
            ->field('bw_user_id, bw_status, bw_type, bw_price')
            ->find();
        if($withdraw['bw_status'] != 1) return json(['code' => 100, 'msg' => '记录状态异常，请刷新列表重试']);

        $user_id = $withdraw['bw_user_id'];
        $huo_balance = $withdraw['bw_price'];


        Db::startTrans();
        try{
            // 更新申请状态
            Db::table('up_bo_withdraw')
                ->update([
                    'bw_id' => $bw_id,
                    'bw_status' => 2,
                    'bw_check_time' => date('Y-m-d H:i:s'),
                    'bw_admin_id' => session('username')
                ]);
            
            // 如果提现到货款，更新用户积分&入明细表，直开、团长分佣、地区代理管理费
            if($withdraw['bw_type'] == 2){
                // 1.提现到补货款
                $user = Db::table('up_u_user')
                    ->where('user_id', $user_id)
                    ->field([
                        'user_bo_balance',
                        'user_bo_invite_other',
                        'user_bo_role',
                        'user_real_name',
                        'user_up_tz_code',
                        'user_up_up_tz_code',
                        'user_sheng',
                        'user_shi',
                        'user_qu'
                    ])
                    ->find();
                //*************************  积分转补货款   start ****************************//
                // 1.用户补货款
                $this->insertBoInte($user_id, 1, '佣金提现转货款', $huo_balance, $user['user_bo_balance'], 0);

                // 用户积分
                Db::table('up_u_user')
                    ->where('user_id', $user_id)
                    ->inc('user_bo_total_balance', $huo_balance)
                    ->inc('user_bo_balance', $huo_balance)
                    ->inc('user_bo_huo_balance', $huo_balance)
                    ->update();
                //*************************  补货款充值   end ****************************//

                //*************************  团长分佣   start ****************************//
                // 2.本团/上级分佣
                $upHuoFyUser = $this->getBoUserSystemHuo(1, $user['user_bo_role'], $user['user_up_tz_code'], $user['user_up_up_tz_code']);
                if($upHuoFyUser['should_user_id'] != 0){
//                                $huoTzFy1 = $huo_balance * $upHuoFyUser['ratio'] / 100; // 分佣金额
                    $huoTzFy1 = $huo_balance * config('default_huo_tz_ratio') / 100; // 分佣金额
                    $descFyHuo1 = $user['user_real_name'] . '佣金转货款-佣金';

                    $this->insertBoFy($upHuoFyUser['tz_user_id'], $user_id, $upHuoFyUser['should_user_id'], $upHuoFyUser['freeze_user_id'], 1, $descFyHuo1, 1, $huoTzFy1, $upHuoFyUser['bo_balance'], 0, $upHuoFyUser['hf'], 1, $upHuoFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $upHuoFyUser['tz_user_id'])
                        ->inc('user_bo_fy_balance', $huoTzFy1)
                        ->inc('user_bo_total_fy_balance', $huoTzFy1)
                        ->update();
                }

                // 3.上级/上上级团队长分佣
                $upUpHuoFyUser = $this->getBoUserSystemHuo(2, $user['user_bo_role'], $user['user_up_tz_code'], $user['user_up_up_tz_code']);
                if($upUpHuoFyUser['should_user_id'] != 0){
//                                $huoTzFy2 = $huo_balance * $upUpHuoFyUser['ratio'] / 100; // 分佣金额
                    $huoTzFy2 = $huo_balance * config('default_huo_tz_ratio') / 100; // 分佣金额
                    $descFyHuo2 = $user['user_real_name'] . '佣金转货款-佣金';

                    $this->insertBoFy($upUpHuoFyUser['tz_user_id'], $user_id, $upUpHuoFyUser['should_user_id'], $upUpHuoFyUser['freeze_user_id'], 1, $descFyHuo2, 1, $huoTzFy2, $upUpHuoFyUser['bo_balance'], 0, $upUpHuoFyUser['hf'], 1, $upUpHuoFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $upUpHuoFyUser['tz_user_id'])
                        ->inc('user_bo_fy_balance', $huoTzFy2)
                        ->inc('user_bo_total_fy_balance', $huoTzFy2)
                        ->update();
                }
                //*************************  团长分佣   end ****************************//

                //*************************  代理分佣   start ****************************//
                // 4.县代管理费
                $quFyUser = $this->getDaiLiFy(3, $user['user_qu']);
                if($quFyUser['should_user_id'] != 0){
//                                $huoDlFy3 = $huo_balance * $quFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy3 = $huo_balance * config('default_qu_ratio') / 100; // 分佣金额
                    $descFyHuo3_dl = $user['user_real_name'] . '佣金转货款-县管理费';

                    $this->insertBoFy($quFyUser['fy_user_id'], $user_id, $quFyUser['should_user_id'], 0, 1, $descFyHuo3_dl, 1, $huoDlFy3, $quFyUser['bo_balance'], 0, $quFyUser['hf'], 2, $quFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $quFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy3)
                        ->inc('user_bo_total_fy_balance', $huoDlFy3)
                        ->update();
                }

                // 5.市代管理费
                $shiFyUser = $this->getDaiLiFy(2, $user['user_shi']);
                if($shiFyUser['should_user_id'] != 0){
//                                $huoDlFy2 = $huo_balance * $shiFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy2 = $huo_balance * config('default_shi_ratio') / 100; // 分佣金额
                    $descFyHuo2_dl = $user['user_real_name'] . '佣金转货款-市管理费';

                    $this->insertBoFy($shiFyUser['fy_user_id'], $user_id, $shiFyUser['should_user_id'], 0, 1, $descFyHuo2_dl, 1, $huoDlFy2, $shiFyUser['bo_balance'], 0, $shiFyUser['hf'], 3, $shiFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $shiFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy2)
                        ->inc('user_bo_total_fy_balance', $huoDlFy2)
                        ->update();
                }

                // 6.省代管理费
                $shengFyUser = $this->getDaiLiFy(1, $user['user_sheng']);
                if($shengFyUser['should_user_id'] != 0){
//                                $huoDlFy1 = $huo_balance * $shengFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy1 = $huo_balance * config('default_sheng_ratio') / 100; // 分佣金额
                    $descFyHuo1_dl = $user['user_real_name'] . '佣金转货款-省管理费';

                    $this->insertBoFy($shengFyUser['fy_user_id'], $user_id, $shengFyUser['should_user_id'], 0, 1, $descFyHuo1_dl, 1, $huoDlFy1, $shengFyUser['bo_balance'], 0, $shengFyUser['hf'], 4, $shengFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $shengFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy1)
                        ->inc('user_bo_total_fy_balance', $huoDlFy1)
                        ->update();
                }
                //*************************  代理分佣   end ****************************//
            }
            
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 审核拒绝
     * @return mixed|\think\response\Json
     */
    public function bwRefuse(){
        $bw_id = input('bw_id');
        if(Request::instance()->isPost()){
            $reason = trim(input('reason'));
            if(!$reason) $reason = '佣金提现退回';
            $withdraw = Db::table('up_bo_withdraw')->where('bw_id', $bw_id)
                ->field('bw_user_id, bw_status, bw_price, bw_type')
                ->find();
            if($withdraw['bw_status'] != 1) return json(['code' => 100, 'msg' => '记录状态异常，请刷新列表重试']);
           
            $user = Db::table('up_u_user')
                ->where('user_id', $withdraw['bw_user_id'])
                ->field('user_bo_fy_balance')
                ->find();
            // 如果是提现到银行卡，需要考虑减去已经转积分的部分
            if($withdraw['bw_type'] == 1){
                $zhuan = Db::table('up_bo_withdraw_to_integral')->where('bwi_bw_id', $bw_id)->sum('bwi_price');
                $withdraw['bw_price'] = $withdraw['bw_price'] - $zhuan;
            }
            
            Db::startTrans();
            try{
                // 更新申请状态
                Db::table('up_bo_withdraw')
                    ->update([
                        'bw_id' => $bw_id,
                        'bw_status' => 3,
                        'bw_refuse_reason' => $reason,
                        'bw_check_time' => date('Y-m-d H:i:s'),
                        'bw_admin_id' => session('username')
                    ]);

                // 积分明细
                $desc = '佣金提现退回-' . $reason;
                $this->insertBoFy($withdraw['bw_user_id'], 0, $withdraw['bw_user_id'], 0, 1, $desc, 4, $withdraw['bw_price'], $user['user_bo_fy_balance'], 0, 2, 0);

                // 更新用户信息
                Db::table('up_u_user')
                    ->update([
                        'user_id' => $withdraw['bw_user_id'],
                        'user_bo_fy_balance' => $user['user_bo_fy_balance'] + $withdraw['bw_price']
                    ]);

                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        return $this->fetch('', ['bw_id' => $bw_id]);
    }
    
    /**
     * 转积分列表
     * @return mixed|\think\response\Json
     */
    public function bwToIntegral(){
        if(request()->isAjax()){
            $where['bwi_bw_id'] = session('bw_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_withdraw_to_integral')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_bo_withdraw_to_integral')
                ->field('bwi_id, bwi_price, bwi_time')
                ->where($where)
                ->order('bwi_time desc')
                ->page($Nowpage,$limits)
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $bw_id = input('bw_id');
        if(isset($bw_id) && $bw_id != '') session('bw_id', $bw_id);
        $status = Db::table('up_bo_withdraw')->where('bw_id', session('bw_id'))->value('bw_status');
        return $this->fetch('', ['status' => $status]);
    }
    
    /**
     * 转积分
     * @return mixed|\think\response\Json
     */
    public function toIntegral(){
        $withdraw = Db::table('up_bo_withdraw')
            ->where('bw_id', session('bw_id'))
            ->field('bw_price, bw_user_id, bw_binte_id')
            ->find();
        $already_zhuan = Db::table('up_bo_withdraw_to_integral')->where('bwi_bw_id', session('bw_id'))->sum('bwi_price');
        $can_zhuan = $withdraw['bw_price'] - $already_zhuan;
        if(Request::instance()->isPost()){
            $bwi_price = input('bwi_price');
            $user_id = $withdraw['bw_user_id'];
            if($bwi_price > $can_zhuan) return json(['code' => 100, 'msg' => '超出可转积分']);

            $user = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->field([
//                        'user_bo_total_balance',
                    'user_bo_balance',
//                        'user_bo_huo_balance',
                    'user_bo_invite_other',
                    'user_bo_role',
                    'user_real_name',
                    'user_up_tz_code',
                    'user_up_up_tz_code',
                    'user_sheng',
                    'user_shi',
                    'user_qu'
                ])
                ->find();

            Db::startTrans();
            try{
                // 插入转积分明细
                Db::table('up_bo_withdraw_to_integral')
                    ->insert([
                        'bwi_binte_id' => $withdraw['bw_binte_id'],
                        'bwi_bw_id' => session('bw_id'),
                        'bwi_price' => $bwi_price,
                        'bwi_user_id' => $user_id,
                        'bwi_time' => date('Y-m-d H:i:s')
                    ]);

                //*************************  提现到银行卡-部分转货款   start ****************************//
                // 1.用户补货款
                $this->insertBoInte($user_id, 1, '提现到银行卡-部分转货款', $bwi_price, $user['user_bo_balance'], 0);

                // 用户积分
                Db::table('up_u_user')
                    ->where('user_id', $user_id)
                    ->inc('user_bo_total_balance', $bwi_price)
                    ->inc('user_bo_balance', $bwi_price)
                    ->inc('user_bo_huo_balance', $bwi_price)
                    ->update();
                //*************************  补货款充值   end ****************************//

                //*************************  团长分佣   start ****************************//
                // 2.本团/上级分佣
                $upHuoFyUser = $this->getBoUserSystemHuo(1, $user['user_bo_role'], $user['user_up_tz_code'], $user['user_up_up_tz_code']);
                if($upHuoFyUser['should_user_id'] != 0){
//                                $huoTzFy1 = $huo_balance * $upHuoFyUser['ratio'] / 100; // 分佣金额
                    $huoTzFy1 = $bwi_price * config('default_huo_tz_ratio') / 100; // 分佣金额
                    $descFyHuo1 = $user['user_real_name'] . '提现到银行卡-部分转货款-佣金';

                    $this->insertBoFy($upHuoFyUser['tz_user_id'], $user_id, $upHuoFyUser['should_user_id'], $upHuoFyUser['freeze_user_id'], 1, $descFyHuo1, 1, $huoTzFy1, $upHuoFyUser['bo_balance'], 0, $upHuoFyUser['hf'], 1, $upHuoFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $upHuoFyUser['tz_user_id'])
                        ->inc('user_bo_fy_balance', $huoTzFy1)
                        ->inc('user_bo_total_fy_balance', $huoTzFy1)
                        ->update();
                }

                // 3.上级/上上级团队长分佣
                $upUpHuoFyUser = $this->getBoUserSystemHuo(2, $user['user_bo_role'], $user['user_up_tz_code'], $user['user_up_up_tz_code']);
                if($upUpHuoFyUser['should_user_id'] != 0){
//                                $huoTzFy2 = $huo_balance * $upUpHuoFyUser['ratio'] / 100; // 分佣金额
                    $huoTzFy2 = $bwi_price * config('default_huo_tz_ratio') / 100; // 分佣金额
                    $descFyHuo2 = $user['user_real_name'] . '提现到银行卡-部分转货款-佣金';

                    $this->insertBoFy($upUpHuoFyUser['tz_user_id'], $user_id, $upUpHuoFyUser['should_user_id'], $upUpHuoFyUser['freeze_user_id'], 1, $descFyHuo2, 1, $huoTzFy2, $upUpHuoFyUser['bo_balance'], 0, $upUpHuoFyUser['hf'], 1, $upUpHuoFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $upUpHuoFyUser['tz_user_id'])
                        ->inc('user_bo_fy_balance', $huoTzFy2)
                        ->inc('user_bo_total_fy_balance', $huoTzFy2)
                        ->update();
                }
                //*************************  团长分佣   end ****************************//

                //*************************  代理分佣   start ****************************//
                // 4.县代管理费
                $quFyUser = $this->getDaiLiFy(3, $user['user_qu']);
                if($quFyUser['should_user_id'] != 0){
//                                $huoDlFy3 = $huo_balance * $quFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy3 = $bwi_price * config('default_qu_ratio') / 100; // 分佣金额
                    $descFyHuo3_dl = $user['user_real_name'] . '提现到银行卡-部分转货款-县管理费';

                    $this->insertBoFy($quFyUser['fy_user_id'], $user_id, $quFyUser['should_user_id'], 0, 1, $descFyHuo3_dl, 1, $huoDlFy3, $quFyUser['bo_balance'], 0, $quFyUser['hf'], 2, $quFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $quFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy3)
                        ->inc('user_bo_total_fy_balance', $huoDlFy3)
                        ->update();
                }

                // 5.市代管理费
                $shiFyUser = $this->getDaiLiFy(2, $user['user_shi']);
                if($shiFyUser['should_user_id'] != 0){
//                                $huoDlFy2 = $huo_balance * $shiFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy2 = $bwi_price * config('default_shi_ratio') / 100; // 分佣金额
                    $descFyHuo2_dl = $user['user_real_name'] . '提现到银行卡-部分转货款-市管理费';

                    $this->insertBoFy($shiFyUser['fy_user_id'], $user_id, $shiFyUser['should_user_id'], 0, 1, $descFyHuo2_dl, 1, $huoDlFy2, $shiFyUser['bo_balance'], 0, $shiFyUser['hf'], 3, $shiFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $shiFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy2)
                        ->inc('user_bo_total_fy_balance', $huoDlFy2)
                        ->update();
                }

                // 6.省代管理费
                $shengFyUser = $this->getDaiLiFy(1, $user['user_sheng']);
                if($shengFyUser['should_user_id'] != 0){
//                                $huoDlFy1 = $huo_balance * $shengFyUser['ratio'] / 100; // 分佣金额
                    $huoDlFy1 = $bwi_price * config('default_sheng_ratio') / 100; // 分佣金额
                    $descFyHuo1_dl = $user['user_real_name'] . '提现到银行卡-部分转货款-省管理费';

                    $this->insertBoFy($shengFyUser['fy_user_id'], $user_id, $shengFyUser['should_user_id'], 0, 1, $descFyHuo1_dl, 1, $huoDlFy1, $shengFyUser['bo_balance'], 0, $shengFyUser['hf'], 4, $shengFyUser['freeze_name']);

                    Db::table('up_u_user')
                        ->where('user_id', $shengFyUser['fy_user_id'])
                        ->inc('user_bo_fy_balance', $huoDlFy1)
                        ->inc('user_bo_total_fy_balance', $huoDlFy1)
                        ->update();
                }
                //*************************  代理分佣   end ****************************//
                
                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        
        return $this->fetch('', ['can_zhuan' => $can_zhuan]);
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $bw_ids = input('ids');

        $where = [];
        if($bw_ids == -1){
            $bw_number = trim(input('bw_number'), ' '); // 提现单号
            $user_username = trim(input('user_username')); // 用户名称
            $user_phone = trim(input('user_phone')); // 手机号
            $start_time = input('start_time');
            $end_time = input('end_time');
            $bw_status = input('bw_status');

            if(isset($bw_number) && $bw_number != ''){
                $where['bw_number'] = ['like', '%' . $bw_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_real_name'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bw_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bw_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bw_time'] = ['<=', $end_time];
            }
            if(isset($bw_status) && $bw_status != ''){
                $where['bw_status'] = $bw_status;
            }
        }else{
            if($bw_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['bw_id'] = ['IN', $bw_ids];
        }

        $lists = Db::table('up_bo_withdraw')
            ->join('up_u_user', 'bw_user_id = user_id', 'LEFT')
            ->field('bw_id, bw_status, bw_type, bw_number, bw_price, bw_refuse_reason, 
                bw_time, user_real_name, user_phone, user_bank_username, user_bank_name, user_bankcard_code, 
                bw_check_time')
            ->where($where)
            ->order('bw_time desc')
            ->select();
    
        foreach($lists as $k => $v){
            // 申请状态
            if($v['bw_status'] == 1){
                $lists[$k]['bw_status'] = '待审核';
            }elseif($v['bw_status'] == 2){
                $lists[$k]['bw_status'] = '审核通过';
            }else{
                $lists[$k]['bw_status'] = '审核拒绝';
            }
            // 申请种类
            $lists[$k]['bw_type'] = $v['bw_type'] == 1 ? '提现到银行卡' : '转积分';
            // 订单号
//            $lists[$k]['withdraw_number'] = "\t" . $v['withdraw_number'] . "\t";
            // 价格
            $lists[$k]['bw_price'] = "\t" . $v['bw_price'] . "\t";
            // 手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            // 银行卡号
            $lists[$k]['user_bankcard_code'] = "\t" . $v['user_bankcard_code'] . "\t";
        }
        $cellname = [
            ['user_real_name', '申请用户', 10, 'LEFT'],
            ['bw_number', '提现单号', 25, 'LEFT'],
            ['bw_price', '提现金额', 20, 'LEFT'],
            ['bw_status', '提现状态', 20, 'LEFT'],
            ['bw_type', '提现类型', 20, 'LEFT'],
            ['user_real_name', '真实姓名', 20, 'LEFT'],
            ['user_phone', '手机号', 20, 'LEFT'],
            ['user_bank_username', '银行账户名', 20, 'LEFT'],
            ['user_bank_name', '开户行', 20, 'LEFT'],
            ['user_bankcard_code', '卡号', 25, 'LEFT'],
            ['bw_refuse_reason', '拒绝原因', 20, 'LEFT'],
            ['bw_time', '申请时间', 20, 'LEFT'],
            ['bw_check_time', '审核时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('提现', 'Bo_withdraw', $cellname, $lists);
        return json($res);
    }
}